<template>
	<view class="container">
		<view class="title">设置登录密码</view>

		<!-- 密码输入框 -->
		<view class="input-group">
			<input :password="!setting.iseye" text v-model="setting.password" placeholder="请输入密码" class="input" />
			<view class="input-group-fixed">
				<uni-icons class="clear-icon" v-show="setting.password" @click="setting.password = ''" type="closeempty" size="20"></uni-icons>
				<uni-icons class="eye-icon" @click="setting.iseye = !setting.iseye" :type="setting.iseye ? 'eye' : 'eye-slash'" size="22"></uni-icons>
			</view>
		</view>
		<!-- 密码规则提示 -->
		<view class="password-tip">密码长度6-20位，数字/字母/符号</view>

		<!-- 确认按钮 -->
		<button class="submit-btn xjs-btn-primary" @click="handleSubmit">确认提交</button>
		<view v-if="setting.param.register" @click="toJump" class="tg">跳过</view>
	</view>
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app';
import { ref, reactive, computed, watch } from 'vue';
import { useLogin } from '@/hooks/useLogin';
import api from '@/api';
onLoad((params) => {
	setting.param = params;
});
const setting = reactive<any>({
	password: '',
	iseye: false,
	param: {}
});
const handleSubmit = async () => {
	let blankSpaceReg = /^\S*$/; //非空格正则
	let loginPswReg = /^.{6,20}$/; //密码正则
	let notzhongwen = /^[^\u4e00-\u9fa5]+$/;
	if (!setting.password) {
		uni.showToast({
			title: '密码不能为空',
			icon: 'none'
		});
		return;
	}
	if (!blankSpaceReg.test(setting.password)) {
		uni.showToast({
			title: '密码中不可输入空格',
			icon: 'none'
		});
		return;
	}
	if (!loginPswReg.test(setting.password) || !notzhongwen.test(setting.password)) {
		uni.showToast({
			title: '密码错误，请重新输入',
			icon: 'none'
		});
		return;
	}
	let res: any = null;
	if (setting.param.register) {
		res = await api({
			method: 'put',
			url: `/mall/user/setPwdFirst`,
			data: {
				pwd: setting.password
			}
		});
	} else {
		res = await api({
			method: 'put',
			url: `/mall/user/pwd`,
			data: {
				pwd: setting.password,
				code: setting.param.verifyCode,
				countryCode: setting.param.areaCode,
				phone: setting.param.phone,
				id: setting.param.userId
			}
		});
	}

	if (res.code !== 0) {
		uni.showToast({
			title: res.msg,
			icon: 'none'
		});
		return;
	} else {
		uni.showToast({
			title: '设置成功',
			icon: 'none',
			mask: true,
			duration: 1000
		});
		setTimeout(() => {
			uni.reLaunch({
				url: setting.param.register?'/pages/home/index':'/pages/login/index'
			});
		}, 1000);
	}
};

const toJump = () => {
	uni.reLaunch({
		url: '/pages/home/index'
	});
};
</script>

<style lang="scss" scoped>
.container {
	padding: 160rpx 48rpx 48rpx;
	min-height: 100vh;
	background-color: #fff;
	box-sizing: border-box;

	.title {
		font-size: 40rpx;
		font-weight: bold;
		text-align: center;
		margin-bottom: 60rpx;
	}

	.password-tip {
		font-size: 24rpx;
		color: #999;
		margin-top: -10rpx;
		margin-bottom: 40rpx;
	}

	.submit-btn {
		margin: 80rpx 0;
	}
	.tg {
		text-align: center;
		font-size: 24rpx;
		color: #666;
	}
}
</style>
